package com.zanbo.tikNotes;

import java.math.BigDecimal;
import java.text.DecimalFormat;

/**
 * 金钱转换
 *
 * @author zanbo
 * @program Solution
 * @since 2021-12-30 上午 9:41
 */
public class FormatMoney {
    public static double b2s(String str) {
        str = str.replaceAll("零", "");
        double sum = 0;
        int index = str.indexOf("亿");
        if (index > 0) {
            String temp = str.substring(0, index);
            str = str.substring(index + 1);
            sum = sum + w(temp) * 100000000;
        }
        index = str.indexOf("万");
        if (index > 0) {
            String temp = str.substring(0, index);
            str = str.substring(index + 1);
            sum = sum + w(temp) * 10000;
        }
        index = str.indexOf("元");
        if (index > 0) {
            String temp = str.substring(0, index);
            str = str.substring(index + 1);
            sum = sum + w(temp);
        }
        index = str.indexOf("角");
        if (index > 0) {
            String temp = str.substring(0, index);
            str = str.substring(index + 1);
            sum = sum + str2num(temp) * 0.1;
        }
        index = str.indexOf("分");
        if (index > 0) {
            String temp = str.substring(0, index);
            str = str.substring(index + 1);
            sum = sum + str2num(temp) * 0.01;
        }

        return sum;
    }

    /**
     * 大写转小写
     *
     * @param str
     * @return
     */
    public static int str2num(String str) {
        String[] daxie = {"壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
        int[] num = {1, 2, 3, 4, 5, 6, 7, 8, 9};
        for (int i = 0; i < daxie.length; i++) {
            if (str.equals(daxie[i])) {
                return num[i];
            }
        }
        return 0;
    }

    /**
     * @param str
     * @return
     * @see 转万以下的
     */
    public static double w(String str) {
        double sum = 0;
        int index = str.indexOf("仟");
        if (index > 0) {
            String temp = str.substring(0, index);
            str = str.substring(index + 1);
            sum = sum + str2num(temp) * 1000;
        }
        index = str.indexOf("佰");
        if (index > 0) {
            String temp = str.substring(0, index);
            str = str.substring(index + 1);
            sum = sum + str2num(temp) * 100;

        }
        index = str.indexOf("拾");
        if (index > 0) {
            String temp = str.substring(0, index);
            str = str.substring(index + 1);
            sum = sum + str2num(temp) * 10;
        }
        if (str.length() > 0) {
            sum = sum + str2num(str);
        }
        return sum;
    }

    public static void main(String[] args) {
        DecimalFormat df = new DecimalFormat();
        df.setMinimumFractionDigits(2);
        df.setMaximumFractionDigits(2);
        System.out.println(df.format(b2s("壹仟万壹仟伍佰壹拾玖元捌角陆分")));
        double d1 = 0.10334;
        double d2 = 1234.0;

        System.out.println("new BigDecimal("+d1+")=" + new BigDecimal(d1)); //此种方式绝对不允许!!!!!
        System.out.println("new BigDecimal("+d2+")=" + new BigDecimal(d2)); //此种方式绝对不允许!!!!!
        System.out.println("");

        System.out.println("new BigDecimal(String.valueOf("+d1+"))=" + new BigDecimal(String.valueOf(d1)));
        System.out.println("new BigDecimal(String.valueOf("+d2+"))=" + new BigDecimal(String.valueOf(d2)));
        System.out.println("");

        System.out.println("new BigDecimal(String.valueOf("+d1+"))=" + new BigDecimal(Double.toString(d1)));
        System.out.println("new BigDecimal(String.valueOf("+d2+"))=" + new BigDecimal(Double.toString(d2)));
        System.out.println("");

        System.out.println("BigDecimal.valueOf("+d1+")=" + BigDecimal.valueOf(d1));
        System.out.println("BigDecimal.valueOf("+d2+")=" + BigDecimal.valueOf(d2));
        System.out.println("");

        BigDecimal b1 = BigDecimal.valueOf(1.0);
        BigDecimal b2 = BigDecimal.valueOf(1.00000);
        System.out.println(b1.equals(b2));
        System.out.println(b1.compareTo(b2));
    }
}
